package com.example.bestudentsystem.demos.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bestudentsystem.demos.web.mapper.*;
import com.example.bestudentsystem.demos.web.pojo.Course;
import com.example.bestudentsystem.demos.web.pojo.Student;
import com.example.bestudentsystem.demos.web.pojo.Teacher;
import com.example.bestudentsystem.demos.web.vo.CourseVo;
import com.example.bestudentsystem.demos.web.vo.SCVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class teacherController {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    SCMapper scMapper;

    // 根据教师的 tno 获取教师教授的课程
    @GetMapping("/getCoursesByTno")
    public List<Course> getCoursesByTno(@RequestParam String tno){
        return teacherMapper.getCourseByTno(tno);
    }

    // 获取教师的个人信息
    @GetMapping("/getMyTData")
    public Teacher getTeacherData(@RequestParam String id) {
        return teacherMapper.selectById(id);
    }

    // 获取某个课程下学生的成绩
    @GetMapping("/getStudentScoreByCno")
    public IPage<SCVo> getStudentScoreByCno(@RequestParam("page") Integer page,
                                            @RequestParam("size") Integer size,
                                            @RequestParam("cno") String cno,
                                            @RequestParam("sno") String sno) {
        Page<SCVo> iPage = new Page<>(page, size);
        return studentMapper.getStudentScoreByCno(iPage, cno, sno);
    }

    // 获取某个班级的学生数据
    @GetMapping("/getStudentDataByClass")
    public IPage<Student> getStudentDataByClass(@RequestParam("page") Integer page,
                                                @RequestParam("size") Integer size,
                                                @RequestParam("tno") String tno,
                                                @RequestParam("name") String name) {
        Page<Student> iPage = new Page<>(page, size);
        return studentMapper.getStudentDataByClass(iPage, tno, name);
    }

    // 获取班级学生的成绩
    @GetMapping("/getStudentScoreByClass")
    public IPage<SCVo> getStudentScoreByClass(@RequestParam("page") Integer page,
                                              @RequestParam("size") Integer size,
                                              @RequestParam("tno") String tno,
                                              @RequestParam("sno") String sno) {
        Page<SCVo> iPage = new Page<>(page, size);
        return studentMapper.getStudentScoreByClass(iPage, tno, sno);
    }

    // 更新学生成绩
    @PutMapping("/updateScore")
    public void updateScore(@RequestParam("cno") String cno,
                            @RequestParam("sno") String sno,
                            @RequestParam("score") Integer score) {
        scMapper.updateScore(score, sno, cno);
        System.out.println("成绩修改成功");
    }

    // 更新学生出勤
    @PutMapping("/updateAttendance")
    public void updateAttendance(@RequestParam("cno") String cno,
                                 @RequestParam("sno") String sno,
                                 @RequestParam("attendance") float attendance) {
        scMapper.updateAttendance(attendance, sno, cno);
        System.out.println("出勤修改成功");
    }

    // 获取可以申请的课程列表（没有被分配教师的课程）
    @GetMapping("/teacher/courses/available")
    public List<CourseVo> getAvailableCourses() {
        // 先更新 isopen 为 3 的课程状态为 0
        teacherMapper.resetIsOpenToZero();
        // 然后查询 tno=00000 且 isopen=0 的课程
        return teacherMapper.getNullCourse();
    }

    // 提交课程发布申请（更新课程的名额和申请状态）
    @PutMapping("/teacher/request")
    public void requestCourse(@RequestParam("tno") String tno,
                              @RequestParam("residue") int residue,
                              @RequestParam("apply") int apply,
                              @RequestParam("cname") String cname){
        teacherMapper.Update(tno, residue, apply, cname);
    }

    // 获取教师已提交的课程申请（包括已申请的课程）
    @GetMapping("/teacher/pending")
    public List<CourseVo> getPendingCourse(@RequestParam("tno") String tno){
        return teacherMapper.getAllCourseByTno(tno);
    }
    @PutMapping("/updateMyTData")
    public void updateMyTData(@RequestBody Teacher teacher) {
        teacherMapper.updateById(teacher);
    }
}
